package com.dssrz.weibo4j.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @类功能说明: 中文编码过滤器，过滤请求URL中的中文字符
 * @类修改者:     
 * @修改日期:   
 * @修改说明:   
 * @作者:       robin
 * @创建时间:   2011-8-12 上午11:46:52
 * @版本:       1.0.0
 */
public class EncodingFilter implements Filter {

	private String targetEncoding;
	protected FilterConfig filterConfig = null;
	
	// 由Web容器调用，销毁此Filter
	public void destroy() {
		this.targetEncoding = null;
		this.filterConfig = null;
	}

	// 过滤处理方法
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
			FilterChain chain) throws IOException, ServletException {
		// 对请求进行编码设置
		HttpServletRequest request = (HttpServletRequest)servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		request.setCharacterEncoding(targetEncoding);
		// 将处理权转交给下一个处理器
		chain.doFilter(request, response);
	}

	// 由Web容器调用，初始化此Filter
	public void init(FilterConfig config) throws ServletException {
		this.filterConfig = config;
		this.targetEncoding = config.getInitParameter("encoding");
	}

	public void setFilterConfig(final FilterConfig filterConfig){
		this.filterConfig = filterConfig;
	}

}
